package com.weirong.smartvault.backservice.entity_model;

import com.weirong.smartvault.common.tools.constant.MoneyEntityConstant;
import com.weirong.smartvault.common.tools.spring.context.SpringContextUtil;
import com.weirong.smartvault.backservice.util.common.ReflectTool;
import com.weirong.smartvault.dao.ForeignCurrencyTypeRepository;
import com.weirong.smartvault.entity.ForeignCurrencyType;

import java.lang.reflect.InvocationTargetException;

/**
 * 于2017/12/6由 KFEB4 创建 .
 */
public class MoneyLogModel<T> {
    T log;
    ForeignCurrencyTypeRepository foreignCurrencyTypeRepository;
    public MoneyLogModel(T log) {
        this.log = log;
        foreignCurrencyTypeRepository = SpringContextUtil.getBean(ForeignCurrencyTypeRepository.class);
    }
    public Integer getDenominationType() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return (Integer) ReflectTool.runMethod(log, MoneyEntityConstant.getDenominationValue);
    }

    public Integer getAmount() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return (Integer) ReflectTool.runMethod(log, MoneyEntityConstant.getAmount);
    }

    public Integer getSum() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return getAmount()*getDenominationType();
    }

    public String getCurrencyType() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return (String) ReflectTool.runMethod(log, MoneyEntityConstant.getTypeCode);
    }

    public String getCurrencyName() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, NoSuchFieldException {
        String typeCode = getCurrencyType();
        ForeignCurrencyType foreignCurrencyType = foreignCurrencyTypeRepository.getByCode(typeCode);
        return foreignCurrencyType.getName();
    }
}
